package util.old;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.File;

/**
 * Servlet implementation class util.old.javacmd
 */
@WebServlet("/util.old.javacmd")
public class javacmd extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public javacmd() {
        super();
        //System.out.println(util.old.Test.exec("python yolov5/detect.py --source yolov5/data/images --weight yolov5/yolov5s.pt"));
        //File file = new File("C:\\Users\\1575280002\\Desktop\\runs");
        //System.out.println("result��"+util.old.DelFiles.delFiles(file));
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.getWriter().append("Served at: ").append(request.getContextPath());
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}



class Test {
    /**
     * ���÷���
     * @param cmd cmd����
     * @return
     */
    public static String exec(String cmd) {
        try {
            Runtime rt = Runtime.getRuntime();
            Process proc = rt.exec(cmd);
            InputStream es = proc.getErrorStream();
            String line;
            BufferedReader br;
            br = new BufferedReader(new InputStreamReader(es, "GBK"));
            StringBuffer buffer=new StringBuffer();
            while ((line = br.readLine()) != null) {
                buffer.append(line+"\n");
            }
            return buffer.toString();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
/**
 * ɾ��ĳ��Ŀ¼��Ŀ¼�µ�������Ŀ¼���ļ�
 * @author : harara
 * @version : 2.0
 * @date : 2020/5/19 17:04
 */
class DelFiles {

    /**
     * �ݹ�ɾ��
     * ɾ��ĳ��Ŀ¼��Ŀ¼�µ�������Ŀ¼���ļ�
     * @param file �ļ���Ŀ¼
     * @return ɾ�����
     */
    public static boolean delFiles(File file){
        boolean result = false;
        //Ŀ¼
        if(file.isDirectory()){
            File[] childrenFiles = file.listFiles();
            for (File childFile:childrenFiles){
                result = delFiles(childFile);
                if(!result){
                    return result;
                }
            }
        }
        //ɾ�� �ļ�����Ŀ¼
        result = file.delete();
        return result;
    }
}
